#include <stdio.h>

#include "nx_secure_tls_api.h"

#include "tls_test_utility.h"

extern void    test_control_return(UINT status);


void NX_Secure_TLS_AlertTest();

#ifdef CTEST
void test_application_define(void *first_unused_memory);
void test_application_define(void *first_unused_memory)
#else
void nx_secure_tls_alert_test_application_define(void *first_unused_memory)
#endif
{

    /* Print out test information banner.  */
    printf("NetX Secure Test:   TLS Alert mapping Test.............................");

    NX_Secure_TLS_AlertTest();

    printf("SUCCESS!\n");
    test_control_return(0);
}

#define TLS_ALERT_TEST(error, expected_alert, expected_level)                      \
            _nx_secure_tls_map_error_to_alert(error, &alert_number, &alert_level); \
            EXPECT_EQ(expected_alert, alert_number);                               \
            EXPECT_EQ(expected_level, alert_level);      

TEST(NX_Secure_TLS, AlertTest)
{
UINT alert_number;
UINT alert_level;


    /* Unsupported or otherwise incorrect alerts.
        NX_SECURE_TLS_ALERT_EXPORT_RESTRICTION_RESERVED
        NX_SECURE_TLS_ALERT_INSUFFICIENT_SECURITY      
        NX_SECURE_TLS_ALERT_USER_CANCELED              
        NX_SECURE_TLS_ALERT_ACCESS_DENIED              
        NX_SECURE_TLS_ALERT_DECRYPTION_FAILED_RESERVED 
        NX_SECURE_TLS_ALERT_RECORD_OVERFLOW            
        NX_SECURE_TLS_ALERT_DECOMPRESSION_FAILURE      
        NX_SECURE_TLS_ALERT_NO_CERTIFICATE_RESERVED    
        NX_SECURE_TLS_ALERT_UNSUPPORTED_EXTENSION    
    */

    /* Unexpected message alerts. */
    TLS_ALERT_TEST(NX_SECURE_TLS_UNRECOGNIZED_MESSAGE_TYPE, NX_SECURE_TLS_ALERT_UNEXPECTED_MESSAGE, NX_SECURE_TLS_ALERT_LEVEL_FATAL); 
    TLS_ALERT_TEST(NX_SECURE_TLS_ALERT_RECEIVED           , NX_SECURE_TLS_ALERT_UNEXPECTED_MESSAGE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);  
    TLS_ALERT_TEST(NX_SECURE_TLS_BAD_CIPHERSPEC           , NX_SECURE_TLS_ALERT_UNEXPECTED_MESSAGE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);  

    /* Hash or decryption failures. */
    TLS_ALERT_TEST(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE , NX_SECURE_TLS_ALERT_BAD_RECORD_MAC, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_AEAD_DECRYPT_FAIL, NX_SECURE_TLS_ALERT_BAD_RECORD_MAC, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_PADDING_CHECK_FAILED, NX_SECURE_TLS_ALERT_BAD_RECORD_MAC, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* General handshake failures. */
    TLS_ALERT_TEST(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_UNSUPPORTED_CIPHER, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_NO_SUPPORTED_CIPHERS, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* Invalid certificate issues. */
    TLS_ALERT_TEST(NX_SECURE_TLS_INVALID_SERVER_CERT, NX_SECURE_TLS_ALERT_BAD_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_INVALID_CERTIFICATE, NX_SECURE_TLS_ALERT_BAD_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_CERTIFICATE_SIG_CHECK_FAILED, NX_SECURE_TLS_ALERT_BAD_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_CERTIFICATE_NOT_FOUND, NX_SECURE_TLS_ALERT_BAD_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_X509_WRONG_SIGNATURE_METHOD, NX_SECURE_TLS_ALERT_BAD_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_X509_INVALID_DATE_FORMAT, NX_SECURE_TLS_ALERT_BAD_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_X509_ASN1_LENGTH_TOO_LONG, NX_SECURE_TLS_ALERT_BAD_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_X509_PKCS7_PARSING_FAILED, NX_SECURE_TLS_ALERT_BAD_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* Unsupported certificate issues (unsupported ciphers and signature types). */
    TLS_ALERT_TEST(NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER, NX_SECURE_TLS_ALERT_UNSUPPORTED_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_UNSUPPORTED_CERT_SIGN_TYPE, NX_SECURE_TLS_ALERT_UNSUPPORTED_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_UNSUPPORTED_CERT_SIGN_ALG, NX_SECURE_TLS_ALERT_UNSUPPORTED_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* A certificate was revoked by its signer. */
    TLS_ALERT_TEST(NX_SECURE_X509_CRL_CERTIFICATE_REVOKED, NX_SECURE_TLS_ALERT_CERTIFICATE_REVOKED, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* A certificate has expired or is not yet valid. */
    TLS_ALERT_TEST(NX_SECURE_X509_CERTIFICATE_EXPIRED, NX_SECURE_TLS_ALERT_CERTIFICATE_EXPIRED, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_X509_CERTIFICATE_NOT_YET_VALID, NX_SECURE_TLS_ALERT_CERTIFICATE_EXPIRED, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* Unknown certificate issues - the certificate was unsupported but for some odd reason (or it was self-signed). */
    TLS_ALERT_TEST(NX_SECURE_TLS_INVALID_SELF_SIGNED_CERT, NX_SECURE_TLS_ALERT_CERTIFICATE_UNKNOWN, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_UNKNOWN_CERT_SIG_ALGORITHM, NX_SECURE_TLS_ALERT_CERTIFICATE_UNKNOWN, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /*  Illegal parameters - bad compression method, etc. */
    TLS_ALERT_TEST(NX_SECURE_TLS_BAD_COMPRESSION_METHOD, NX_SECURE_TLS_ALERT_ILLEGAL_PARAMETER, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* The issuer for a received certificate was not found in our local store. */
    TLS_ALERT_TEST(NX_SECURE_TLS_ISSUER_CERTIFICATE_NOT_FOUND, NX_SECURE_TLS_ALERT_UNKNOWN_CA, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_X509_CHAIN_VERIFY_FAILURE, NX_SECURE_TLS_ALERT_UNKNOWN_CA, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* Some type of decoding error happened with a received message. */
    TLS_ALERT_TEST(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH, NX_SECURE_TLS_ALERT_DECODE_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* Decryption error in processing a message. */
    TLS_ALERT_TEST(NX_SECURE_TLS_FINISHED_HASH_FAILURE, NX_SECURE_TLS_ALERT_DECRYPT_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_SIGNATURE_VERIFICATION_ERROR, NX_SECURE_TLS_ALERT_DECRYPT_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* We received a protocol version that we understand but that version is not supported/enabled. */
    TLS_ALERT_TEST(NX_SECURE_TLS_PROTOCOL_VERSION_CHANGED, NX_SECURE_TLS_ALERT_PROTOCOL_VERSION, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_UNSUPPORTED_TLS_VERSION, NX_SECURE_TLS_ALERT_PROTOCOL_VERSION, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_UNKNOWN_TLS_VERSION, NX_SECURE_TLS_ALERT_PROTOCOL_VERSION, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* Re-negotiation issues - the client may opt to decline a Hello Request message. */
    TLS_ALERT_TEST(NX_SECURE_TLS_NO_RENEGOTIATION_ERROR, NX_SECURE_TLS_ALERT_NO_RENEGOTIATION, NX_SECURE_TLS_ALERT_LEVEL_WARNING);

    /* Unknown PSK errors. */
    TLS_ALERT_TEST(NX_SECURE_TLS_NO_MATCHING_PSK, NX_SECURE_TLS_ALERT_UNKNOWN_PSK_IDENTITY, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* Unexpected messages. */
    TLS_ALERT_TEST(NX_SECURE_TLS_UNEXPECTED_MESSAGE, NX_SECURE_TLS_ALERT_UNEXPECTED_MESSAGE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);               

    /* Bad certificate issues. */
    TLS_ALERT_TEST(NX_SECURE_TLS_CERTIFICATE_VERIFY_FAILURE, NX_SECURE_TLS_ALERT_BAD_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_EMPTY_REMOTE_CERTIFICATE_RECEIVED, NX_SECURE_TLS_ALERT_BAD_CERTIFICATE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* Renegotiation errors. */   
    TLS_ALERT_TEST(NX_SECURE_TLS_RENEGOTIATION_FAILURE, NX_SECURE_TLS_ALERT_NO_RENEGOTIATION, NX_SECURE_TLS_ALERT_LEVEL_WARNING);            

    /* Fallback (TLS protocol version) errors. */
    TLS_ALERT_TEST(NX_SECURE_TLS_INAPPROPRIATE_FALLBACK, NX_SECURE_TLS_ALERT_INAPPROPRIATE_FALLBACK, NX_SECURE_TLS_ALERT_LEVEL_FATAL);           
    
    /* Missing extensions, groups, verification errors resulting in handshake failure. */
    TLS_ALERT_TEST(NX_SECURE_TLS_EXTENSION_NOT_FOUND, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);              
    TLS_ALERT_TEST(NX_SECURE_TLS_SNI_EXTENSION_INVALID, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);            
    TLS_ALERT_TEST(NX_SECURE_TLS_EMPTY_EC_GROUP, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);                   
    TLS_ALERT_TEST(NX_SECURE_TLS_EMPTY_EC_POINT_FORMAT, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);            
    TLS_ALERT_TEST(NX_SECURE_TLS_UNSUPPORTED_ECC_CURVE, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);            
    TLS_ALERT_TEST(NX_SECURE_TLS_UNSUPPORTED_ECC_FORMAT, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);           
    TLS_ALERT_TEST(NX_SECURE_TLS_UNSUPPORTED_SIGNATURE_ALGORITHM, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);  
    TLS_ALERT_TEST(NX_SECURE_TLS_UNSUPPORTED_FEATURE          , NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_RENEGOTIATION_SESSION_INACTIVE, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    TLS_ALERT_TEST(NX_SECURE_TLS_RENEGOTIATION_EXTENSION_ERROR, NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE, NX_SECURE_TLS_ALERT_LEVEL_FATAL);

    /* Record overflow. */
    TLS_ALERT_TEST(NX_SECURE_TLS_RECORD_OVERFLOW, NX_SECURE_TLS_ALERT_RECORD_OVERFLOW, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
    
    /* Internal errors. */
    TLS_ALERT_TEST(NX_SECURE_TLS_ALLOCATE_PACKET_FAILED       , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL); 
    TLS_ALERT_TEST(NX_SECURE_TLS_SESSION_UNINITIALIZED        , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);  
    TLS_ALERT_TEST(NX_SECURE_TLS_INVALID_STATE                , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_INVALID_PACKET               , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_NEED_DTLS_SESSION            , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_NEED_TLS_SESSION             , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_INSUFFICIENT_CERT_SPACE      , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_TCP_SEND_FAILED              , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_NO_CLOSE_RESPONSE            , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_NO_MORE_PSK_SPACE            , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_NO_CERT_SPACE_ALLOCATED      , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_CLOSE_NOTIFY_RECEIVED        , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_CRYPTO_KEYS_TOO_LARGE        , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);            
    TLS_ALERT_TEST(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL      , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);          
    TLS_ALERT_TEST(NX_SECURE_TLS_CERT_ID_INVALID              , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);                  
    TLS_ALERT_TEST(NX_SECURE_TLS_CERT_ID_DUPLICATE            , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);                
    TLS_ALERT_TEST(NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE       , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);           

    /* DTLS errors. */
    TLS_ALERT_TEST(NX_SECURE_TLS_OUT_OF_ORDER_MESSAGE   , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_INVALID_REMOTE_HOST    , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);       
    TLS_ALERT_TEST(NX_SECURE_TLS_INVALID_EPOCH          , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);       
    TLS_ALERT_TEST(NX_SECURE_TLS_REPEAT_MESSAGE_RECEIVED, NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);       
    TLS_ALERT_TEST(NX_SECURE_TLS_SEND_ADDRESS_MISMATCH  , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_NO_FREE_DTLS_SESSIONS  , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_DTLS_SESSION_NOT_FOUND     , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);    
    TLS_ALERT_TEST(NX_SECURE_TLS_NO_AVAILABLE_SESSIONS  , NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);       

    TLS_ALERT_TEST(NX_SECURE_TLS_SUCCESS, NX_SECURE_TLS_ALERT_INTERNAL_ERROR, NX_SECURE_TLS_ALERT_LEVEL_FATAL);
}
